package com.atguigu.lease.web.admin.controller.login;


import com.atguigu.lease.common.Redis.RedisConstant;
import com.atguigu.lease.common.context.LoginSysUser;
import com.atguigu.lease.common.context.LoginSysUserContext;
import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String key = RedisConstant.ADMIN_LOGIN_PREFIX+ UUID.randomUUID();
        String vercode = specCaptcha.text().toLowerCase();
        redisTemplate.opsForValue().set(key,vercode,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        CaptchaVo captchaVo = new CaptchaVo(specCaptcha.toBase64(),key);
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        if (!StringUtils.hasText(loginVo.getCaptchaCode())) {
            return Result.ok("未输入验证码");
        }
        String s = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if (s == null){
            return Result.ok("验证码已过期");
        }
        if (!loginVo.getCaptchaCode().equalsIgnoreCase(s)) {
            return Result.ok("验证码错误");
        }

        SystemUser user = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, loginVo.getUsername()));
        if(user==null){
            return Result.ok("用户不存在");
        }
        if(user.getStatus()== BaseStatus.DISABLE){
            return Result.ok("用户已被禁用");
        }
        if(!user.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))){
            return Result.ok("密码错误");
        }

        String token = jwtUtil.createToken(user.getId(), user.getUsername());
        return Result.ok(token);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info() {
        LoginSysUser contex = LoginSysUserContext.getContex();
        SystemUser user = systemUserService.getById(contex.getUserid());
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setAvatarUrl(user.getAvatarUrl());
        systemUserInfoVo.setName(user.getName());
        return Result.ok(systemUserInfoVo);
    }
}